<!-- user表单弹窗 -->
<form id="video-form" lay-filter="video-form" class="layui-form model-form" enctype=“multipart/form-data”>
    <input name="id" type="hidden"/>
    <div class="layui-form-item">
        <label class="layui-form-label">视频名称</label>
        <div class="layui-input-block">
            <input name="fileName" placeholder="请输入文件名" id="fileName" type="text" class="layui-input" maxlength="20"
                   lay-verify="required" required/>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">文件地址</label>
        <div class="layui-input-block">
            <input name="file" placeholder="请选择文件" id="file" type="file" class="layui-input"
                   required/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">公司</label>
        <div class="layui-input-block">
            <select id="company" name="company" filter lay-verify="required">
            </select>
        </div>
    </div>
    <div class="layui-form toolbar">
    </div>
<!--    <div class="layui-form-item">-->
<!--        <label class="layui-form-label">上传视频</label>-->
<!--        <button type="button" class="layui-btn cover" id="control_video_button"  id="fileBtn">上传视频</button>-->
<!--        <input type="hidden" id="control_video" value="{$detail.control_video}"  name="control_video"/>-->
<!--        <input type="hidden" id="temp_url_id" value="" name="temp_url_id"/>-->
<!--    </div>-->

    <div class="layui-form-item">
        <label class="layui-form-label">文件类型</label>
        <div class="layui-input-block">
            <input name="type" placeholder="请输入文件类型" id="type" type="text" class="layui-input"
                   required/>
        </div>
    </div>
    <div class="layui-form-item model-form-footer">
        <button class="layui-btn layui-btn-primary" ew-event="closeDialog" type="button">取消</button>
        <button class="layui-btn" lay-filter="video-form-submit" lay-submit>保存</button>
    </div>
</form>

<script>
    $(function () {

    });
    layui.use(['layer', 'admin', 'form', 'formSelects','upload','config'], function () {
        var layer = layui.layer;
        var admin = layui.admin;
        var form = layui.form;
        var formSelects = layui.formSelects;
        var upload=layui.upload;
        var config=layui.config;
        form.render('radio');
        //
        // b64EncodeUnicode($("#file"))
        // //文件上传
        // upload.render({
        //     elem: '#fileBtn' ,
        //     url:  config.base_server + 'api-videos/video/upload',
        //     accept: 'file',
        //     size:"20000",
        //     auto: true ,
        //     field:"layuiVideo",
        //     before: function(obj){
        //         layer.load(); //上传loading
        //         this.data={access_token:config.getToken().access_token
        //         };
        //     },
        //     done: function(data){
        //         layer.closeAll('loading');
        //         // console.log(data);
        //         $("[name=uri]").val(data.data);
        //     },
        //     error: function (data) {
        //         layer.closeAll('loading');
        //     }
        // });
                var video = admin.getTempData('t_video');
                $('#video-form').attr('method', 'POST');
                    form.val('video-form', video);

        console.log("ssss")
        // 表单提交事件
        var token=JSON.parse(layui.data("easyweb").token).access_token;
        let html='';
        console.log("nihao")
        $.ajax({
            url: config.base_server +"api-videos/video/selectCompany",
            type: "post",
            dataType: "json",
            data: {access_token:token},
            success: function (data) {
                if (data.code == 0) {
                    for (let i = 0; i < data.data.length; i++) {
                        html = html + ' <option value=" ' + data.data[i].id + '">' + data.data[i].companyName + '</option>'
                    }
                    console.log(html);
                    $("#company").html(html);
                    form.render("select", "video-form");
                }
            }
        })

        form.on('submit(video-form-submit)', function (data) {
            layer.load(2);
            // var file =$("#file")[0].files
            // data.file=file
            if(data.field.id!=null&&data.field.id!=''){

                admin.req('api-videos/video/edit', JSON.stringify(data.field), function (data) {
                    layer.closeAll('loading');
                    if (data.code == 0) {
                        layer.msg(data.msg, {icon: 1, time: 500});
                        admin.finishPopupCenter();
                    } else {
                        layer.msg(data.msg, {icon: 2, time: 500});
                    }
                }, 'post');
            }

            else {
                //上传视频，转码为base64格式
                var counts="";
                fileToBase64($("#file")[0].files[0],function (callback) {
                    counts=callback.result;

                    var datas={fileName:$("#fileName").val(),file:callback ,type:$("#type").val(),companyId:$("#company").val()}
                    admin.req('api-videos/video/pushVideo', JSON.stringify(datas), function (data) {
                        layer.closeAll('loading');
                        if (data.code == 0) {
                            layer.msg(data.msg, {icon: 1, time: 500});
                            admin.finishPopupCenter();
                        } else {
                            layer.msg(data.msg, {icon: 2, time: 500});
                        }
                    }, 'post');
                })

            }
            return false;
        });
    });




    function fileToBase64(file, callback) {
        const fileReader = new FileReader()
        fileReader.readAsDataURL(file)
        fileReader.onload = function () {
            callback(this.result)
        }
    }
</script>